package com.yanqu.road.server.cmd.tradewar.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.bussiness.player.UserPatronsBussiness;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Map;

@Cmd(code = Protocol.S_CROSS_TRADE_WAR_QUERY_ENEMY_PATRONS, desc = "跨服商战活查询对手门客信息")
public class CrossTradeWarQueryEnemyPatronsCmd extends AbstractCommand {

    private int activityId;
    private long enemyUserId;
    private long enemyServerId;
    private Map<Integer, UserPatrons> patronsMap;
    private BigInteger enemyAbility = BigInteger.ZERO;
    private boolean needResp = true;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossTradeWarProto.CrossTradeWarQueryEnemyPatronsReqMsg reqMsg = CrossTradeWarProto.CrossTradeWarQueryEnemyPatronsReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        enemyUserId = reqMsg.getEnemyUserId();
        enemyServerId = reqMsg.getEnemyServerId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!TradeWarActivityMgr.canCrossTradeWar(player.getUserId())){//没资格商战
            ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_NO_QUALIFIED;
            return;
        }
        if(enemyUserId <= 0){
            ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_PARAM_ERROR;
        }
        if(GameServer.getInstance().getServerId() == enemyServerId){
            GamePlayer enemyPlayer = GamePlayerMgr.getOnlinePlayer(enemyUserId);
            if(null != enemyPlayer){
                patronsMap = enemyPlayer.getModule(PatronsModule.class).getUserPatronsMap();
            }else {
                patronsMap = UserPatronsBussiness.getUserPatronsMap(enemyUserId);
            }
            enemyAbility = PatronsMgr.getPatronsListAbility(new ArrayList<>(patronsMap.values()));
        }else {
            ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo();
            if(null == activityInfo){
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
            if(!activityInfo.getServerIdList().contains(enemyServerId)){
                ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_PARAM_ERROR;
                return;
            }
            if(!ServerListMgr.isServerProvideServices(enemyServerId)){
                ret = GameErrorCode.E_CROSS_TRADE_WAR_ACTIVITY_ENEMY_SERVER_MAINTAIN;
                return;
            }
            CrossTradeWarProto.CrossTradeWarQueryEnemyPatronsReqMsg.Builder queryMsg = CrossTradeWarProto.CrossTradeWarQueryEnemyPatronsReqMsg.newBuilder();
            queryMsg.setActivityId(activityId);
            queryMsg.setEnemyUserId(enemyUserId);
            queryMsg.setEnemyServerId(enemyServerId);
            player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_TRADE_WAR_QUERY_ENEMY_PATRONS, queryMsg, activityId);
            needResp = false;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(needResp) {
            CrossTradeWarProto.CrossTradeWarQueryEnemyPatronsRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarQueryEnemyPatronsRespMsg.newBuilder();
            respMsg.setRet(ret);
            if(0 == ret){
                for(UserPatrons userPatrons : patronsMap.values()){
                    PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
                    respMsg.addEnemyPatronsList(patronsMsg);
                }
                respMsg.setEnemyPatronsAbility(enemyAbility.toString());
            }
            player.sendPacket(Protocol.U_CROSS_TRADE_WAR_QUERY_ENEMY_PATRONS, respMsg);
        }
    }
}
